//
//  main.m
//  05-循环遍历
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
#if 0
    {
    NSArray *stringArr = @[@"高压线", @"机讲机", @"指环王", @"手机机", @"啊丽哟"];
    NSMutableArray *mutArr = [NSMutableArray array];
    for (NSInteger i = (stringArr.count - 1); i >= 0; i--) {
        NSLog(@"%@", stringArr[i]);
        [mutArr addObject:stringArr[i]];
    }
    NSLog(@"%@", mutArr);
    
    NSDictionary *dic = @{@"name": @"水杯", @"sex": @"男", @"age": @20};
    NSArray *allKeys = [dic allKeys];
    for (int i = 0; i < allKeys.count; i++) {
        id value = [dic objectForKey:allKeys[i]];
    }
    
#pragma mark - 枚举器NSEnumerator
    // 枚举器(遍历方式)
    // NSEnumerator 依赖于集合类型(数组 字典 集合) 不能单独使用
    NSEnumerator *arrEnumerator = [stringArr objectEnumerator];
    id value = nil;
    while (value = [arrEnumerator nextObject]) {
        NSLog(@"value = %@", value);
    }
    // 倒序枚举
    NSEnumerator *reverse = [stringArr reverseObjectEnumerator];
    id value1 = nil;
    while (value1 = [reverse nextObject]) {
        NSLog(@"value1 = %@", value1);
    }
    // 字典枚举器
    // 默认取出来的值是value dic.allKeys能取出来所有key
    NSEnumerator *dicEnumerator = [dic objectEnumerator];
    id dicValue = nil;
    while (dicValue = [dicEnumerator nextObject]) {
        NSLog(@"%@", dicValue);
    }
    // 集合枚举器
    NSSet *set = [NSSet setWithObjects:@"102", @"10054", @"110", nil];
    NSEnumerator *setEnumerator = [set objectEnumerator];
    id setValue = nil;
    while (setValue = [setEnumerator nextObject]) {
        NSLog(@"setvalue = %@", setValue);
    }
    
    NSString *newString = [[NSString alloc] init];
    // 快速枚举 for ... in (对NSEnumerator的封装)
    for (NSString *string in stringArr) {
        newString = [newString stringByAppendingString:string];
    }
    NSLog(@"%@", newString);
    
    // 快速枚举一个字典
    for (NSString *key in dic) {
        
    }
    
    // 快速枚举一个集合
    for (NSString *anyObject in set) {
        
    }
    
#pragma mark - 排序描述符
    // 参数1: 排序条件 参数二 acending: YES(升序) or NO(降序)
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
    NSArray *arr = [stringArr sortedArrayUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", arr);
    
    NSMutableArray *mutableArr = [NSMutableArray arrayWithObjects:@120, @115, @130, @140, nil];
    [mutableArr sortUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", mutableArr);
    // 排序: 可变数组和不可变数组通过sort排序调用的方法
    // 不可变数组,排序完成后返回新的数组
    // 可变数组,排序直接在本身发生改变
    
#pragma mark - 数组中元素为对象时排序
    Person *personOne = [[Person alloc] init];
    personOne.name = @"AF";
    personOne.age = 30;
    Person *personTwo = [[Person alloc] init];
    personTwo.name = @"AF";
    personTwo.age = 15;
    Person *personThree = [[Person alloc] init];
    personThree.name = @"AF";
    personThree.age = 25;
    
    NSArray *personArr = @[personOne, personTwo, personThree];
    NSSortDescriptor *sortNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSSortDescriptor *sortAgeDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    
    personArr =[personArr sortedArrayUsingDescriptors:@[sortNameDescriptor, sortAgeDescriptor]];
    NSLog(@"%@", personArr);
    }
#endif
    
#pragma mark - 方法选择器排序
    NSArray *sortString = @[@"iPhone", @"iPad", @"iTouch", @"iMac", @"Mac Mini"];
    sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@", sortString);
    
    return 0;
}
